package com.zzyl.common.mytest;

import lombok.Data;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

public class BaiduQianfanClient {

    // Lombok 数据类：请求体
    @Data
    public static class ChatRequest {
        private String model;
        private Message[] messages;
    }

    @Data
    public static class Message {
        private String role;
        private String content;

        public Message(String role, String content) {
            this.role = role;
            this.content = content;
        }
    }

    // Lombok 数据类：响应体（简化版，可根据实际响应结构调整）
    @Data
    public static class ChatResponse {
        private String id;
        private String object;
        private long created;
        private String result; // 假设返回结果字段是 "result"，请根据实际 API 文档调整
        // 可添加 choices, usage 等字段
    }

    public static void main(String[] args) {
        // 1. 创建 RestTemplate 实例（不依赖 Spring 容器）
        RestTemplate restTemplate = new RestTemplate();

        // 2. 构造请求 URL
        String url = "https://qianfan.baidubce.com/v2/chat/completions";

        // 3. 构造请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setBearerAuth("bce-v3/ALTAK-eWUi7p40VNVUIr0eeno0m/a5556823f443d826acc6a007536c3f7ed2efb3e2\n" ); // 替换为你的实际 token

        // 4. 构造请求体
        ChatRequest request = new ChatRequest();
        request.setModel("ernie-lite-8k");
        request.setMessages(new Message[]{
                new Message("system", "平台助手"),
                new Message("user", "你好")
        });

        // 5. 包装成 HttpEntity
        HttpEntity<ChatRequest> entity = new HttpEntity<>(request, headers);

        // 6. 发送 POST 请求并接收响应
        ResponseEntity<ChatResponse> response = restTemplate.postForEntity(url, entity, ChatResponse.class);

        // 7. 输出结果
        if (response.getStatusCode().is2xxSuccessful()) {
            System.out.println("✅ 请求成功，响应内容：");
            System.out.println(response.getBody());
        } else {
            System.out.println("❌ 请求失败，状态码：" + response.getStatusCode());
            System.out.println("响应体：" + response.getBody());
        }
    }
}
